Puzzle 14 Explanation: Short Variables
Understand why multiple variable declarations matter.
We'll cover the following
Try it yourself#
Try running the code below to see the result.
Multiple assigning variables
Explanation#
In Go, the short variable declaration (:=) isused in a multivalue context, where there is more than one variable on the left side.
If all variables on the left are new, you’re good:
If you have no new variables, you’ll get a compilation error:
When there’s a mix, like in this teaser, our code will be okay as long as the types match. In this case, we write the following:
In the above line, b is an existing variable, and the type of 3, which is int, matches the current type of b. Here, c is a new variable, making this statement OK.
Puzzle 14: Multiple Personalities
Puzzle 15: A Tale of Two Cities